use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(i32)]
pub enum TaskStatus {
    Pending = 0,
    Running = 1,
    Completed = 2,
}

impl Default for TaskStatus {
    fn default() -> Self {
        Self::Pending
    }
}

impl From<i32> for TaskStatus {
    fn from(value: i32) -> Self {
        match value {
            0 => Self::Pending,
            1 => Self::Running,
            2 => Self::Completed,
            _ => Self::Pending,
        }
    }
}

impl From<TaskStatus> for i32 {
    fn from(status: TaskStatus) -> Self {
        status as i32
    }
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "spider_task")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub name: String,
    pub target_url: String,
    pub url_pattern: String,
    pub article_list_selector: String,
    pub article_title_selector: String,
    pub article_selector: String,
    pub breadcrumb_selector: String,
    pub status: i32,
    pub last_run_at: i64,
    pub crawled_count: i64,
    pub created_at: i64,
    pub updated_at: i64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SpiderTaskDTO {
    pub id: i32,
    pub name: String,
    pub target_url: String,
    pub url_pattern: String,
    pub article_list_selector: String,
    pub article_title_selector: String,
    pub article_selector: String,
    pub breadcrumb_selector: String,
    pub status: i32,
    pub last_run_at: Option<i64>,
    pub crawled_count: i64,
}